package com.tudouji.project.d3.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.d3.domain.ResponseBack;
import com.tudouji.project.d3.domain.*;
import com.tudouji.project.d3.mapper.D3AftersalesItemsMapper;
import com.tudouji.project.d3.mapper.D3ShopMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.d3.mapper.D3AftersalesMapper;
import com.tudouji.project.d3.service.ID3AftersalesService;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 电商平台售后Service业务层处理
 *
 * @author tudouji
 * @date 2023-08-21
 */
@Slf4j
@Service
public class D3AftersalesServiceImpl implements ID3AftersalesService {
    @Autowired
    private D3AftersalesMapper d3AftersalesMapper;
    @Autowired
    private D3AftersalesItemsMapper d3AftersalesItemsMapper;
    @Autowired
    private D3ShopMapper d3ShopMapper;
    /**
     * 查询电商平台售后
     *
     * @param id 电商平台售后ID
     * @return 电商平台售后
     */
    @Override
    public D3Aftersales selectD3AftersalesById(Long id) {
        return d3AftersalesMapper.selectD3AftersalesById(id);
    }

    /**
     * 查询电商平台售后列表
     *
     * @param d3Aftersales 电商平台售后
     * @return 电商平台售后
     */
    @Override
    public List<D3Aftersales> selectD3AftersalesList(D3Aftersales d3Aftersales) {
        return d3AftersalesMapper.selectD3AftersalesList(d3Aftersales);
    }

    /**
     * 新增电商平台售后
     *
     * @param d3Aftersales 电商平台售后
     * @return 结果
     */
    @Override
    public int insertD3Aftersales(D3Aftersales d3Aftersales) {
        d3Aftersales.setCreateTime(DateUtils.getNowDate());
        return d3AftersalesMapper.insertD3Aftersales(d3Aftersales);
    }

    /**
     * 修改电商平台售后
     *
     * @param d3Aftersales 电商平台售后
     * @return 结果
     */
    @Override
    public int updateD3Aftersales(D3Aftersales d3Aftersales) {
        d3Aftersales.setUpdateTime(DateUtils.getNowDate());
        return d3AftersalesMapper.updateD3Aftersales(d3Aftersales);
    }

    /**
     * 批量删除电商平台售后
     *
     * @param ids 需要删除的电商平台售后ID
     * @return 结果
     */
    @Override
    public int deleteD3AftersalesByIds(Long[] ids) {
        return d3AftersalesMapper.deleteD3AftersalesByIds(ids);
    }

    /**
     * 删除电商平台售后信息
     *
     * @param id 电商平台售后ID
     * @return 结果
     */
    @Override
    public int deleteD3AftersalesById(Long id) {
        return d3AftersalesMapper.deleteD3AftersalesById(id);
    }

    @Override
    public ResponseBack insertAfterOrder(List<D3Aftersales> content) {
        try {
            for (int i = 0; i < content.size(); i++) {
                D3Aftersales d3Aftersales = content.get(i);
                d3Aftersales = d3AftersalesMapper.selectD3AftersalesByRefAid(d3Aftersales.getRefaid());

                D3Shop shop = new D3Shop();
                shop.setCode(d3Aftersales.getD3Poscode());
                List<D3Shop> shops = d3ShopMapper.selectD3ShopList(shop);
                shop = shops.get(0);
                if (null == d3Aftersales) {
                    d3Aftersales.setShopId(shop.getId());
                    d3Aftersales.setCreateTime(DateUtils.getNowDate());
                    d3AftersalesMapper.insertD3Aftersales(d3Aftersales);

                    List<D3AftersalesItems> lines = d3Aftersales.getLines();
                    if (lines.size() > 0) {
                        for (D3AftersalesItems item : lines) {
                            item.setCreateTime(DateUtils.getNowDate());
                            item.setSftersalesId(d3Aftersales.getId());
                            d3AftersalesItemsMapper.insertD3AftersalesItems(item);
                        }
                    }
                }
            }
            return new ResponseBack(true, "200", null);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseBack(false, "E10001", "推送发⽣了错误");
        }
    }

    @Override
    public ResponseBack updateAfterOrder(List<D3Aftersales> content) {
        try {
            for (int i = 0; i < content.size(); i++) {
                D3Aftersales d3Aftersales = content.get(i);
                D3Aftersales order = d3AftersalesMapper.selectD3AftersalesByRefAid(d3Aftersales.getRefaid());
                if (null != order) {
                    d3Aftersales.setId(order.getId());
                    d3AftersalesMapper.updateD3Aftersales(d3Aftersales);
                    d3AftersalesItemsMapper.deleteD3AftersalesItemsByRefAid(order.getRefaid());
                    List<D3AftersalesItems> lines = d3Aftersales.getLines();
                    if (lines.size() > 0) {
                        for (D3AftersalesItems item : lines) {
                            item.setCreateTime(DateUtils.getNowDate());
                            item.setSftersalesId(d3Aftersales.getId());
                            d3AftersalesItemsMapper.insertD3AftersalesItems(item);
                        }
                    }
                } else {
                    log.info("售后订单：" + d3Aftersales.getRefaid() + "，未查找到此订单！");
                    return new ResponseBack(false, "E10003", "售后订单：" + d3Aftersales.getRefoid() + "，未查找到此订单！");
                }
            }
            return new ResponseBack(true, "200", null);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseBack(false, "E10001", "推送发⽣了错误");
        }
    }
}
